package dicky.fortunetelling;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;


import dicky.fortunetelling.logic.BaZi;
import dicky.fortunetelling.logic.DataArray;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class bazi extends Activity {
	private Button btn1;
	private EditText date1,time1;
	private TextView text,text1,text2,text3;
	private ListView listView;
	private ViewAdapter dataAdapter;
	private Calendar cal = Calendar.getInstance();
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat tf = new SimpleDateFormat("HH:mm");
	
	private String tag=bazi.class.getSimpleName();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bazi);
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				bazisearch();
			}
		});
        
        date1=(EditText)findViewById(R.id.date1);
        date1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				DatePickerDialog dg = new DatePickerDialog(
						bazi.this, datelistener, cal
								.get(Calendar.YEAR), cal.get(Calendar.MONTH),
						cal.get(Calendar.DAY_OF_MONTH));
				dg.setTitle("选择日期");
				dg.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});

        time1=(EditText)findViewById(R.id.time1);
        time1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				TimePickerDialog dg = new TimePickerDialog(bazi.this,
						timelistener, cal.get(Calendar.HOUR_OF_DAY), cal
								.get(Calendar.MINUTE), true);
				dg.setTitle("选择时间");
				dg.setButton(TimePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});
        
        text1=(TextView)findViewById(R.id.text1);
        text2=(TextView)findViewById(R.id.text2);
        text3=(TextView)findViewById(R.id.text3);
        listView = (ListView) findViewById(R.id.listview);
    }

	private DatePickerDialog.OnDateSetListener datelistener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			date1.setText(df.format(cal.getTime()));
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timelistener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
			cal.set(Calendar.MINUTE, minute);
			
			time1.setText(tf.format(cal.getTime()));
		}
	};
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null
					&& getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);

	}
	
	private void bazisearch() {
		try {
			String d = date1.getText().toString();
			String t = time1.getText().toString();
			if ("".equals(d) || "".equals(t)) {
				Toast.makeText(bazi.this, "请录入出生日期及时间", Toast.LENGTH_LONG).show();
				return;
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(df.parse(d));
			int h = Integer.valueOf(t.substring(0, 2));
			int hour = 1;
			if (h >= 1) {
				hour = 2;
			}
			if (h >= 3) {
				hour = 3;
			}
			if (h >= 5) {
				hour = 4;
			}
			if (h >= 7) {
				hour = 5;
			}
			if (h >= 9) {
				hour = 6;
			}
			if (h >= 11) {
				hour = 7;
			}
			if (h >= 13) {
				hour = 8;
			}
			if (h >= 15) {
				hour = 9;
			}
			if (h >= 17) {
				hour = 10;
			}
			if (h >= 19) {
				hour = 11;
			}
			if (h >= 21) {
				hour = 12;
			}
			if (h >= 23) {
				hour = 1;
				cal.add(cal.DATE, 1);
			}
			
			BaZi lunar = new BaZi(cal);
			String lunarstr=lunar.toString();
			String animal=lunar.animalsYear();
			String ganzhi=lunar.getYearGanZhi(hour);
			lunarstr+=ganzhi.substring(ganzhi.length()-1, ganzhi.length())+"时";
			text1.setText(lunarstr);
			text2.setText(animal);
			text3.setText(ganzhi);

			HashMap ymap=DataArray.GetYearFate(ganzhi.substring(0, 2));
			HashMap mmap=DataArray.GetMonthFate(lunar.month+"");
			HashMap dmap=DataArray.GetDayFate(lunar.day+"");
			HashMap hmap=DataArray.GetHourFate(hour+"");
			HashMap amap=DataArray.GetAnimalFate(animal);
			
			String strary[]=new String[5];
			strary[0]=ymap.get("title")+"\n\n"+ymap.get("content");
			strary[1]=mmap.get("title")+"\n\n"+mmap.get("content");
			strary[2]=dmap.get("title")+"";
			strary[3]=hmap.get("title")+"";
			strary[4]=amap.get("男")+"\n\n"+amap.get("女");
			
			dataAdapter = new ViewAdapter(bazi.this, R.layout.bazilist,strary);
			listView.setAdapter(dataAdapter);
		} catch (ParseException e) {
			Log.e(tag, e+"");
			Toast.makeText(bazi.this, "八字查询失败", Toast.LENGTH_LONG).show();
		}
	}

	private class ViewAdapter extends BaseAdapter {
		private Context mContext;
		private String[] strary;
		private int textViewResourceId;
	
		public ViewAdapter(Context c,int textViewResourceId, String[] strary) {
			this.mContext = c;
			this.textViewResourceId = textViewResourceId;
			this.strary = strary;
		}
	
		@Override
		public int getCount() {
			return strary.length;
		}
	
		@Override
		public String getItem(int position) {
			return strary[position];
		}
	
		@Override
		public long getItemId(int position) {
			return 0;
		}
	
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater vi = (LayoutInflater) bazi.this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(textViewResourceId, null);
			}
			String str=getItem(position);
			
			text=(TextView) convertView.findViewById(R.id.text);
			text.setText(str);
			return convertView;
		}
	}
	
}
